﻿using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Text;
using System.Collections.Generic;

namespace Jy123.Web.home.MiniContent
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class Guess : IHttpHandler
    {
        private int userID;
       // protected string guessState = "";//竞猜状态
        public void ProcessRequest(HttpContext context)
        {
            string strUserIdFromCookie = Common.Cookie.getCookie("UserID");
            if (string.IsNullOrEmpty(strUserIdFromCookie))
            {
                context.Response.Redirect("/");
            }
            userID = int.Parse(strUserIdFromCookie);

            StringBuilder sbHtml = new StringBuilder(context.Cache["user_minicontent"].ToString());//sbContent
            string strUserID = context.Request.QueryString["MiniContentUserID"];
            string strUl = string.Empty;
            if (!string.IsNullOrEmpty(strUserID))
            {
                strUserID = strUserID.Substring(0, strUserID.IndexOf("?"));
                strUl = GuessListByUserID(context, Convert.ToInt32(strUserID));
            }
            else
            {
                strUl = GuessListFriendByUserID(context, userID);
            }
            if (strUl.Trim() == "")
            {
                strUl = "暂无动态!";
            }
            sbHtml = sbHtml.Replace("$ContentMini_ul$", strUl);
            context.Response.ContentType = "text/plain";
            context.Response.Write(sbHtml.ToString());
        }
        private string GuessListFriendByUserID(HttpContext context, int userID)
        {
            int currentpage = 1;//当前页
            int pagesize = 10;//页记录大小
            List<int?> count;

            StringBuilder sbList = new StringBuilder();
            string strListli = context.Cache["user_minicontent_ul_guess"].ToString();
            List<Jy123.Model.User_Guess_Main> guessList = new BLL.Guess.GuessList().getGuessList("FUser in(select ffriend from user_friend where fuser ='"+userID+"') and IsCheckup=1", currentpage, pagesize, "*", "OverTime DESC", out count);
            Jy123.Model.User_Main mUser = new Jy123.Model.User_Main();
            Jy123.BLL.User_Main bllUser = new Jy123.BLL.User_Main();
            foreach (Jy123.Model.User_Guess_Main guess in guessList)
            {
                //判断用户是否有头像
                mUser = bllUser.GetModel(guess.FUser);
                string strPortrait = mUser.Portrait;
                strPortrait = new Jy123.BLL.User.SPace().DefaultPortrait(Convert.ToInt32(guess.FUser));

                string str = strListli;
                str = str.Replace("$GuessID$", guess.ID.ToString());
                str = str.Replace("$UserID$", guess.FUser.ToString());
                str = str.Replace("$UserName$",mUser.UserName );
                str = str.Replace("$Portrait$", strPortrait);
                str = str.Replace("$GuessTitle$", guess.Title);
                str = str.Replace("$GuessOption$",  getGuessItem(context,guess.ID));
                str = str.Replace("$PostTime$", guess.OpenTime.ToString());
                sbList.Append(str);
            }

            return sbList.ToString();
        }

        private string GuessListByUserID(HttpContext context, int userID)
        {
            int currentpage = 1;//当前页
            int pagesize = 10;//页记录大小
            List<int?> count;

            StringBuilder sbList = new StringBuilder();
            string strListli = context.Cache["user_minicontent_ul_guess"].ToString();
            List<Jy123.Model.User_Guess_Main> guessList = new BLL.Guess.GuessList().getGuessList("FUser='" + userID + "' and IsCheckup=1", currentpage, pagesize, "*", "OverTime DESC", out count);
            Jy123.Model.User_Main mUser = new Jy123.Model.User_Main();
            Jy123.BLL.User_Main bllUser = new Jy123.BLL.User_Main();
            foreach (Jy123.Model.User_Guess_Main guess in guessList)
            {
                //判断用户是否有头像
                mUser = bllUser.GetModel(guess.FUser);
                string strPortrait = mUser.Portrait;
                strPortrait = new Jy123.BLL.User.SPace().DefaultPortrait(Convert.ToInt32(guess.FUser));

                string str = strListli;
                str = str.Replace("$GuessID$", guess.ID.ToString());
                str = str.Replace("$UserID$", guess.FUser.ToString());
                str = str.Replace("$UserName$",mUser.UserName );
                str = str.Replace("$Portrait$", strPortrait);
                str = str.Replace("$GuessTitle$", guess.Title);
                str = str.Replace("$GuessOption$", getGuessItem(context,guess.ID));
                str = str.Replace("$PostTime$", guess.OpenTime.ToString());
                sbList.Append(str);
            }

            return sbList.ToString();
           
        }

        private string GuessListOption(HttpContext context, int VoteID)
        {
            StringBuilder sbList = new StringBuilder();
            string strHtml = context.Cache["user_votelist_option"].ToString();
            DataTable dtVoteOption = new Jy123.BLL.User.Vote().VoteOptionList(VoteID, 1, 3, "*", "").Tables[0];
            //List<Jy123.Model.User_VoteOption> listOption = new Jy123.BLL.User_VoteOption().GetModelList("FVote='"+VoteID+"'");
            for (int i = 0; i < dtVoteOption.Rows.Count; i++)
            {
                string strRow = strHtml;
                strRow = strRow.Replace("$VoteOptionName$", dtVoteOption.Rows[i]["OptionName"].ToString());
                sbList.Append(strRow);
            }
            return sbList.ToString();
        }
        /// <summary>
        /// 获取竞猜项HTML
        /// </summary>
        /// <returns>竞猜项HTML</returns>
        protected string getGuessItem(HttpContext context,int guessID)
        {
            StringBuilder sbHtml = new StringBuilder();

            string strLi = context.Cache["guess_itemli"].ToString();//竞猜项LI

            List<Jy123.Model.User_Guess_Item> itemList = new BLL.User_Guess_Item().GetModelList("FGuess =" + guessID);

            //foreach (Jy123.Model.User_Guess_Item itemModel in itemList)
            for (int i=0;i<3;i++)
            {
                string str = strLi;
                
                    str = str.Replace("$InputHtml$", context.Cache["guess_itemradio"].ToString())
                        .Replace("$GuessItemID$", itemList[i].ID.ToString());
                    str = str.Replace("$disabled$", "disabled=\"disabled\"");
                    str = str.Replace("$GuessItemName$", itemList[i].Item);


                    str = str.Replace("$GuessItemCount$", new BLL.Guess.GuessInfo().getGuessItemStat(itemList[i].ID));//竞猜项统计
               
                sbHtml.Append(str);
            }

            return sbHtml.ToString();

        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
